﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FastReflectionLib;
using System.Reflection;
using SZHL.Data;
using SZHL.UI.AppCode;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class HDBMManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(HDBMManage).GetMethod(msg.Action.ToUpper());
            HDBMManage model = new HDBMManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加活动报名
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动报名实体json</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDHDBM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_HDBM hdbm = JsonConvert.DeserializeObject<ZH_HDBM>(P1);
            if (hdbm.HDTitle.Trim() == "")
            {
                msg.ErrorMsg = "活动标题不能为空";
                return;
            }
            if (hdbm.HDContent.Trim() == "")
            {
                msg.ErrorMsg = "活动内容不能为空";
                return;
            }
            if (hdbm.HDFQF.Trim() == "")
            {
                msg.ErrorMsg = "活动发起方不能为空";
                return;
            }
            if (P2 != "") // 处理微信上传的图片
            {
                string fids = CommonHelp.ProcessWxIMG(P2, UserInfo.User.UserName);
                if (!string.IsNullOrEmpty(hdbm.Files))
                {
                    hdbm.Files += "," + fids;
                }
                else
                {
                    hdbm.Files = fids;
                }
            }
            if (hdbm.ID == 0)
            {
                hdbm.CRDate = DateTime.Now;
                hdbm.CRUser = UserInfo.User.UserName;
                new ZH_HDBMB().Insert(hdbm);
                //发送消息
                string content = hdbm.HDFQF + "发起了" + hdbm.HDTime.Value.ToString("yyyy-MM-dd") + "在" + hdbm.HDAdress + "以（" + hdbm.HDTitle + "）为主题的活动";
                new JH_Auth_User_CenterB().SendMsg(UserInfo, "hdbm", "活动报名", content, hdbm.ID, hdbm.HDPeople);
            }
            else
            {
                new ZH_HDBMB().Update(hdbm);
            }
            msg.Result = hdbm;
        }
        /// <summary>
        /// 获取活动报名列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动内容</param>
        /// <param name="P2">活动状态 0全部，1进行中，2已结束</param>
        /// <param name="strUserName"></param>
        public void GETUSERHDBMLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSQL = string.Format("select hd.ID,HDTitle,HDFQF,HDPeople,HDTime,HDBMEndTime,HDAdress,hd.CRDate,hd.CRUser,HDContent,item.CRUser itemUser,hd.Files    from ZH_HDBM hd left join  ZH_HDBM_Item  item on  hd.ID=item.HDID and item.CRUser='{0}'  ", UserInfo.User.UserName);
            string strWhere = string.Format(" where (','+HDPeople+','  like '%," + UserInfo.User.UserName + ",%' or  HDPeople='' )"); ;
            if (P1 != "")
            {
                strWhere += string.Format(" And  (HDContent like '%{0}%' or HDTitle like '%{0}%')", P1);
            }
            if (P2 != "0")
            {
                if (P2 == "1")
                {
                    strWhere += string.Format(" And  HDBMEndTime>='{0}'", DateTime.Now.ToShortDateString());
                }
                if (P2 == "2")
                {
                    strWhere += string.Format(" And  HDBMEndTime<'{0}'", DateTime.Now.ToShortDateString());
                }
            }
            string Id = context.Request.QueryString["Id"] ?? "";//获取活动报名详细及评论时传参
            if (Id != "")
            {
                strWhere = string.Format(" Where  hd.Id={0}", Id);
            }

            strWhere += " order by CRDate DESC ";
            DataTable dt = new ZH_HDBMB().GetDTByCommand(strSQL + strWhere);
            string Ids = "";
            string fileIds = "";
            foreach (DataRow row in dt.Rows)
            {
                Ids += row["ID"] + ",";
                fileIds += row["Files"] + ",";
            }
            DataTable dtPL = new DataTable();
            if (Ids.Length > 0)
            {
                dtPL = new JH_Auth_TLB().GetDTByCommand(string.Format("SELECT ID,MSGTLYID,MSGType,MSGContent,CRDate,CRUser,CRUserName  FROM JH_Auth_TL WHERE MSGType='HDBM' AND  MSGTLYID in ({0})", Ids.TrimEnd(',')));
            }
            List<FT_File> FileList = new FT_FileB().GetEntities(d => fileIds.TrimEnd(',').Split(',').Contains(d.ID.ToString())).ToList();
            dt.Columns.Add("PLList", Type.GetType("System.Object"));
            dt.Columns.Add("FileList", Type.GetType("System.Object"));
            foreach (DataRow row in dt.Rows)
            {
                row["PLList"] = dtPL.FilterTable("MSGTLYID=" + row["ID"]);
                if (FileList.Count > 0)
                {
                    string[] fileId = row["Files"].ToString().Split(',');
                    row["FileList"] = FileList.Where(d => fileId.Contains(d.ID.ToString())).ToList();
                }

            }
            msg.Result = dt;
        }
        /// <summary>
        /// 获取活动报名列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动内容</param>
        /// <param name="P2">活动状态 0全部，1进行中，2已结束</param>
        /// <param name="strUserName"></param>
        public void GETUSERHDBMLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = string.Format("   (','+HDPeople+','  like '%,{0},%' or  HDPeople='' )", UserInfo.User.UserName);
            if (P1 != "")
            {
                strWhere += string.Format(" And  (HDContent like '%{0}%' or HDTitle ike '%{0}%')", P1);
            }
            if (P2 != "0")
            {
                if (P2 == "1")
                {
                    strWhere += string.Format(" And  HDBMEndTime>='{0}'", DateTime.Now.ToShortDateString());
                }
                if (P2 == "2")
                {
                    strWhere += string.Format(" And  HDBMEndTime<'{0}'", DateTime.Now.ToShortDateString());
                }
            }
            string Id = context.Request.QueryString["Id"] ?? "";//获取活动报名详细及评论时传参
            if (Id != "")
            {
                strWhere = string.Format(" Where  hd.Id={0}", Id);
            }

            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_HDBM hd left join  ZH_HDBM_Item  item on  hd.ID=item.HDID and  item.CRUser='" + UserInfo.User.UserName + "' ", "hd.ID,HDTitle,HDFQF,HDTime,HDBMEndTime,HDAdress,hd.CRDate,hd.CRUser,HDContent,item.CRUser itemUser,hd.Files ", 8, page, "hd.HDTime desc", strWhere, ref total);

            dt.Columns.Add("BMList", Type.GetType("System.Object"));
            dt.Columns.Add("PLList", Type.GetType("System.Object"));
            dt.Columns.Add("FileList", Type.GetType("System.Object"));
            foreach (DataRow row in dt.Rows)
            {
                row["BMList"] = new ZH_HDBM_ItemB().GetEntities(d => d.HDID.ToString()==row["ID"].ToString());
                row["PLList"] = new ZH_RCTXB().GetDTByCommand("  SELECT ID,MSGTLYID,MSGType,MSGContent,CRDate,CRUser,CRUserName  FROM JH_Auth_TL WHERE MSGType='HDBM' AND  MSGTLYID='" + row["ID"] + "'");
                string[] fileIds = row["Files"].ToString().Split(',');
                row["FileList"] = new FT_FileB().GetEntities(d => fileIds.Contains(d.ID.ToString()));
            }
            msg.Result = dt;
        }
        /// <summary>
        /// 活动数据管理
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动内容</param>
        /// <param name="P2">活动状态 0全部，1进行中，2已结束</param>
        /// <param name="strUserName"></param>
        public void GETHDBMLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSQL = "select ID,HDTitle,HDFQF,HDTime,HDBMEndTime,HDAdress from ZH_HDBM where 1=1 ";
            if (P1 != "")
            {
                strSQL += string.Format(" And  (HDContent like '%{0}%' or HDTitle like '%{0}%')", P1);
            }
            if (P2 != "0")
            {
                if (P2 == "1")
                {
                    strSQL += string.Format(" And  HDBMEndTime>='{0}'", DateTime.Now.ToShortDateString());
                }
                if (P2 == "2")
                {
                    strSQL += string.Format(" And  HDBMEndTime<'{0}'", DateTime.Now.ToShortDateString());
                }
            }
            strSQL += " order by HDTime DESC ";
            msg.Result = new ZH_HDBMB().GetDTByCommand(strSQL);
        }
        /// <summary>
        /// 获取活动报名信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动报名信息ID</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETHDBMMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new ZH_HDBMB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result1 = new ZH_RCTXB().GetDTByCommand("  SELECT ID,MSGTLYID,MSGType,MSGContent,CRDate,CRUser,CRUserName  FROM JH_Auth_TL WHERE MSGType='HDBM' AND  MSGTLYID='" + P1 + "'"); //手机端使用
        }
        /// <summary>
        /// 删除活动报名信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动报名Id</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void DELHDBMMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            if (!new ZH_HDBMB().Delete(d => d.ID.ToString() == P1))
            {
                msg.ErrorMsg = "删除失败";
            }
            else
            {
                new ZH_HDBM_ItemB().Delete(d => d.HDID.ToString() == P1);
            }
        }
        /// <summary>
        /// 删除报名信息(微信)
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报名Id</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void DELHDBMITEMMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            if (!new ZH_HDBM_ItemB().Delete(d => d.ID.ToString() == P1))
            {
                msg.ErrorMsg = "删除失败";
            }
        }
        /// <summary>
        /// 添加报名信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报名信息json</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDHDBMITEM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_HDBM_Item item = JsonConvert.DeserializeObject<ZH_HDBM_Item>(P1);
            if (item.ID != 0)
            {
                new ZH_HDBM_ItemB().Update(item);
            }
            else
            {
                item.CRUser = UserInfo.User.UserName;
                item.CRDate = DateTime.Now;
                new ZH_HDBM_ItemB().Insert(item);
            }
            msg.Result = item;
        }
        /// <summary>
        /// 获取活动报名的报名列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动报名的ID</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETHDBMITEM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new ZH_HDBM_ItemB().GetEntities(d => d.HDID.ToString() == P1);
        }
        /// <summary>
        /// 已报名点击报名加载用户报名详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">活动报名的ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETHDBMITEMBYUSER(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new ZH_HDBM_ItemB().GetEntity(d => d.HDID.ToString() == P1 && d.CRUser == UserInfo.User.UserName);
        }
    }
}